package com.vote.vote.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vote.common.annotation.Anonymous;
import com.vote.common.core.controller.BaseController;
import com.vote.common.core.domain.AjaxResult;
import com.vote.common.core.redis.RedisCache;
import com.vote.common.utils.http.HttpUtils;
import com.vote.vote.domain.TbLunbo;
import com.vote.vote.domain.TbVote;
import com.vote.vote.service.TbLunboService;
import com.vote.vote.service.TbVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * (TbLunbo)表控制层
 *
 * @author makejava
 * @since 2023-10-01 12:56:53
 */
@RestController
@RequestMapping("web")
public class WebController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TbLunboService tbLunboService;
    @Autowired
    private TbVoteService tbVoteService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("init")
    @Anonymous //不需要登陆访问
    public AjaxResult webInitForVote() {
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
        List<TbLunbo> tbLunbos = new ArrayList<>();
        List<TbVote> tbVotes = new ArrayList<>();
        //redis中查询
        tbVotes = redisCache.getCacheList("tbVotes");
        tbLunbos = redisCache.getCacheList("tbLunbos");

        //说明没有数据 去数据库
        if (tbVotes.size() < 1 || tbLunbos.size() < 1) {
            //查询审核通过的视频
            tbLunbos = tbLunboService.getBaseMapper().selectList(null);
            // 获取视频轮播
            tbVotes = tbVoteService.queryList(new TbVote().setvState(2));

            //将数据保存
            redisCache.setCacheList("tbVotes", tbVotes);
            redisCache.setCacheList("tbLunbos", tbLunbos);
        }

        //首页轮播图
        map.put("lunbo", tbLunbos);
        //所有作品
        map.put("vote", tbVotes);
//      state改为2 则为  嘉宾的拜年视频
        //map.put("userInfo")
        return success(map);
    }

    @GetMapping("/demo11")
    @Anonymous
    public AjaxResult remosss() {
        try {
            // 设置Python脚本路径
            String pythonScriptPath = "D:\\ruoyi\\xxx.py";

            // 使用ProcessBuilder执行Python脚本
            ProcessBuilder processBuilder = new ProcessBuilder("python", pythonScriptPath);
            Process process = processBuilder.start();

            // 读取Python脚本的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            System.err.println("reader" + reader);
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 等待脚本执行完成
            int exitCode = process.waitFor();
            System.out.println("Python脚本执行完成，退出码：" + exitCode);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }
}

